<?php
    include_once 'BaseDAO.php';
    include_once '../../object/User.php';
    include_once '../../util/Mapping.php';

    class User_DAO extends BaseDAO
    {
        public function __construct(){

        }
        
        public function checkUser($username,$password)
        {
                $arr_check  =   array();
                $sql = "SELECT is_admin,id FROM user
                        WHERE username='".$username."'
                            AND password='".$password."'";
                
                $rs = parent::query($sql);
                
                while($row = $rs->fetch(PDO::FETCH_OBJ)){
                    $arr_check['is_admin']  =   $row->is_admin;
                    $arr_check['id']   =   $row->id;
                }
                return $arr_check;

        }
        public function getAllUser()
        {
            $listUser = array();
            $sql = "SELECT * FROM user";
            $rs = parent::query($sql);
            while($row = $rs->fetch(PDO::FETCH_OBJ)){
                $user = new User();
                Mapping::mapUser($row, $user);
                array_push($listUser, $user);

            }

            return $listUser;
        }
        public function getUserByID($id)
        {
            
            $sql = "SELECT * FROM user where id=".$id;
            $rs = parent::query($sql);
            $row = $rs->fetch(PDO::FETCH_OBJ);
            
            $user = new User();
            Mapping::mapUser($row, $user);
            return $user;
        }
        public function insertUser($user){
                 $sql = "INSERT INTO user(username, password, is_admin)
                                 VALUES(
                                 '".$user->getusername()."',
                                 '".$user->getPassword()."',
                                 '".$user->getIs_Admin()."')";
               
                parent::execute($sql);
        }
        public function updateUser($user)
        {
                 $sql = "UPDATE user 
                        SET password = '".$user->getPassword()."'
                        WHERE id='".(int)$user->getid()."'";

                parent::execute($sql);
        }
        public function deleteUser($id)
        {
                $sql = "DELETE FROM user where id='".(int)$id."'";
                
                parent::execute($sql);
        }
    }
?>